package com.situ.car.service.Impl;

import com.situ.car.dao.IOrderDao;
import com.situ.car.dao.IReturnDao;
import com.situ.car.dao.IUserDao;
import com.situ.car.dao.Impl.OrdorDaoImpl;
import com.situ.car.dao.Impl.ReturnDaoImpl;
import com.situ.car.dao.Impl.UserDaoImpl;
import com.situ.car.listener.UserExcelListener;
import com.situ.car.pojo.entity.User;
import com.situ.car.pojo.entity.UserData;
import com.situ.car.pojo.query.UserQuery;
import com.situ.car.service.IUserService;
import com.situ.car.util.ExcelUtils;
import com.situ.car.util.PageInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class UserServiceImpl implements IUserService {
    private IUserDao userDao = new UserDaoImpl();
    private IReturnDao returnDao = new ReturnDaoImpl();
    private IOrderDao orderDao = new OrdorDaoImpl();

    @Override
    public PageInfo selectByPage(UserQuery userQuery) {
        List<User> list = userDao.selectByPage(userQuery);
        int totalCount = userDao.selectTotalCount(userQuery);
        return PageInfo.ok(totalCount, list);
    }

    @Override
    public void deleteById(Integer id) {
        returnDao.deleteByUserId(id);
        orderDao.deleteByUserId(id);
        userDao.deleteById(id);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            returnDao.deleteByUserId(Integer.parseInt(id));
            orderDao.deleteByUserId(Integer.parseInt(id));
            userDao.deleteById(Integer.parseInt(id));
        }
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        userDao.updateStatus(id, status);
    }

    @Override
    public Integer selectByAccount(User user) {
        return userDao.selectByAccount(user);
    }

    @Override
    public void add(User user) {
        userDao.add(user);
    }

    @Override
    public User selectById(Integer id) {
        return userDao.selectById(id);
    }

    @Override
    public int update(User user) {
        return userDao.update(user);
    }

    @Override
    public List<User> selectOrder() {
        return userDao.selectOrder();
    }

    @Override
    public Integer totalCost() {
        return userDao.totalCost();
    }

    @Override
    public void exportExcel(HttpServletResponse resp) {
        List<UserData> list = userDao.selectAll();
        ExcelUtils.exportExcel(resp, list, UserData.class, "user");
    }

    @Override
    public void importExcel(HttpServletRequest req) {
        ExcelUtils.importExcel(req, new UserExcelListener(), UserData.class);
    }


}
